package com.maweirdos.ai.app;

import com.maweirdos.ai.core.adviser.ReReadingadvisor;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.client.advisor.vectorstore.QuestionAnswerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class LoveApp {
    private static final String SYSTEM_PROMPT = "扮演深耕恋爱心理领域的专家。开场向用户表明身份，告知用户可倾诉恋爱难题。" +
            "围绕单身、恋爱、已婚三种状态提问：单身状态询问社交圈拓展及追求心仪对象的困扰；" +
            "恋爱状态询问沟通、习惯差异引发的矛盾；已婚状态询问家庭责任与亲属关系处理的问题。" +
            "引导用户详述事情经过、对方反应及自身想法，以便给出专属解决方案。";
    private final ChatClient chatClient;
    public static final String CHAT_MEMORY_RETRIEVE_SIZE_KEY = "chatMemoryRetrieveSize";


    /**
     * 构造器
     * @param dashscopeChatModel
     */
    public LoveApp(ChatModel dashscopeChatModel) {
        // 与1.0.0 M6有所区别，使用MessageWindowChatMemory的builder()方法构建ChatMemory
        ChatMemory inMemoryChatMemory = MessageWindowChatMemory.builder().build();
        chatClient = ChatClient.builder(dashscopeChatModel)
                .defaultSystem(SYSTEM_PROMPT)
                .defaultAdvisors(
                        // 与1.0.0 M6有所区别，使用MessageChatMemoryAdvisor的builder()方法构建MessageChatMemoryAdvisor
                        MessageChatMemoryAdvisor.builder(inMemoryChatMemory).build(),
                        SimpleLoggerAdvisor.builder().build(),
                        ReReadingadvisor.builder().build()
                )
                .build();

    }

    /**
     * 执行对话
     * @param message
     * @param chatId
     * @return
     */
    public String doChat(String message,String chatId) {
        ChatResponse chatResponse = chatClient.prompt()
                .user(message)
                .advisors(
                        advisorSpec -> advisorSpec.param(ChatMemory.CONVERSATION_ID, chatId)
                                .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10)
                ).call()
                .chatResponse();
        String content  = null;
        if (chatResponse != null) {
            content = chatResponse.getResult().getOutput().getText();
        }
        log.info("content: {}", content);
        return content;
    }

    @Resource
    private VectorStore vectorStore;

    /**
     * 使用rag知识库问答
     * @param message
     * @param chatId
     * @return
     */
    public String doChatByRag(String message,String chatId){
        ChatResponse chatResponse = chatClient.prompt()
                .user(message)
                .advisors(
                        advisorSpec -> advisorSpec.param(ChatMemory.CONVERSATION_ID, chatId)
                                .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10)
                )
                // 开启日志
                .advisors(SimpleLoggerAdvisor.builder().build())
                // 应用rag知识库问答
                .advisors(new QuestionAnswerAdvisor(vectorStore))
                .call()
                .chatResponse();
        String content  = null;
        if (chatResponse != null) {
            content = chatResponse.getResult().getOutput().getText();
        }
        log.info("content: {}", content);
        return content;
    }
}
